package lib.self.util.sp;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * description: 数据存储父类
 * author: Steven Xiao
 * date: 2017/4/26
 */
public abstract class SpBase {
    protected String TAG = getClass().getSimpleName();

    protected SharedPreferences mSp = null;

    public SpBase(Context context, String fileName) {
        mSp = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
    }

    public synchronized boolean save(String key, Object value) {
        return SpUtil.save(mSp, key, value);
    }

    public boolean contains(String key) {
        return SpUtil.contains(mSp, key);
    }

    /**
     * 获取数据
     */
    public Integer getInt(String key) {
        return SpUtil.getInt(mSp, key);
    }

    public Integer getInt(String key, int defValue) {
        return SpUtil.getInt(mSp, key, defValue);
    }

    public Long getLong(String key) {
        return SpUtil.getLong(mSp, key);
    }

    public Long getLong(String key, long defValue) {
        return SpUtil.getLong(mSp, key, defValue);
    }

    public String getString(String key) {
        return SpUtil.getString(mSp, key);
    }

    public String getString(String key, String defValue) {
        return SpUtil.getString(mSp, key, defValue);
    }

    public Boolean getBoolean(String key) {
        return SpUtil.getBoolean(mSp, key);
    }

    public Boolean getBoolean(String key, boolean defValue) {
        return SpUtil.getBoolean(mSp, key, defValue);
    }

    /**
     * 清除数据
     */
    public boolean clear() {
        return SpUtil.clear(mSp);
    }

    public boolean remove(String key) {
        return SpUtil.remove(mSp, key);
    }

    public boolean removeKeys(String... keys) {
        return SpUtil.removeKeys(mSp, keys);
    }

    /**
     * 释放内存，不然单例会一直存在，gc不会回收
     */
    public abstract void free();
}
